package oa.hleast.xswl.service.chat.impl;

import com.baidu.aip.speech.AipSpeech;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.xswl.service.chat.AiService;
import org.apache.commons.lang3.ObjectUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author 302
 */
@Service
@Transactional
@Slf4j
@AllArgsConstructor
public class BaiduAiServiceImpl implements AiService {

    private final AipSpeech baiduAipClient;

    @Override
    public String distinguish(MultipartFile multipartFile) {

        JSONObject jsonObject = null;
        byte[] byteData = null;
        String value = "听的可能不太清楚，您能再说一边吗？";
        try {
            byteData = multipartFile.getBytes();
            jsonObject = baiduAipClient.asr(byteData, "wav", 16000, null);
            log.info("百度识别响应数据 = {}", jsonObject.toString());

            //TODO 返回值这里先这样处理，后期转JSON处理一下
            Object result = jsonObject.get("result");
            System.out.println("result => " + result);
            JSONArray objectJsonArray = jsonObject.getJSONArray("result");
            if (ObjectUtils.isNotEmpty(objectJsonArray)) {
                value = objectJsonArray.get(0) + "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return value;
    }
}
